<template>
  <a-drawer
    title="入学参考科目添加"
    width=650
    placement="right"
    :closable="false"
    @close="onClose"
    :visible="referenceManageAddVisiable"
    style="height: calc(100% - 55px);overflow: auto;padding-bottom: 53px;">
    <a-form :form="form">
      <a-row :gutter="48">
          <a-col :sm="{span:12}" :lg="{span:12}" >
            <a-form-item label="科目码">
              <a-input placeholder="请输入..." v-decorator="['kmbm',{initValue:''}]"/>
            </a-form-item>
          </a-col>
  <!--        <a-col :sm="{span:12}" :lg="{span:12}" >
            <a-form-item label="科目类型">
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-decorator="['kmlxm',{initValue:''}]">
                <a-select-option v-for="i in selectMenu.kmlx" :key="i.code" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col> -->
          <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="科目名称">
              <a-input placeholder="请输入..." v-decorator="['kmmc',{initValue:''}]"/>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="所属院系">
              <a-select mode="multiple" optionFilterProp="children" style="width: 100%" :tokenSeparators="[',']"  placeholder="请输入..." v-decorator="['ssyxm',{initValue:''}]">
                <a-select-option v-for="i in selectMenu.yx" :key="i.dwh">{{i.dwh+"-"+ i.dwmc}}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>

      </a-row>
      <a-table
        ref="table"
        size="default"
        rowKey="key"
        bordered
        :columns="columns"
        :dataSource="data"
        :pagination="false"
        :scroll="{ x: 1500 }"
        >
        <template v-for="col in ['smmc', 'smzz','cbrq', 'smcbs']" :slot="col" slot-scope="text, record">
          <div :key="col">
            <a-date-picker v-if="col =='cbrq'" :format="dateFormat" @change="(date, dateString) => handlePressChange(record.key,'cbrq',date, dateString)" />
            <a-input
              style="margin: -5px 0"
              v-else
              :value="text"
              @change="e => handleChange(e.target.value, record.key, col)"
            />
          </div>
        </template>
        <template slot="action" slot-scope="text,record">
          <a @click="handleDelete(record.key)">删除</a>
        </template>
        </a-table>
    </a-form>
    <div style="padding:10px 0;">
      <a-button style="margin-right: .8rem"  type="primary" block @click="handleAddLine">新增行</a-button>
    </div>
    <div class="drawer-bootom-button">
      <a-button style="margin-right: .8rem" @click="onClose">取消</a-button>
      <a-button @click="handleSubmit" type="primary" :loading="loading">保存</a-button>
    </div>
  </a-drawer>
</template>

<script>
// import moment from 'moment'
import { STable } from '~'
import { mapState } from 'vuex'

const formItemLayout = {
  labelCol: { span: 3 },
  wrapperCol: { span: 18 }
}

const selectArr = ['TYPE0126', 'TYPE0075', 'TYPE0125', 'TYPE0072']
export default {
  props: {
    referenceManageAddVisiable: {
      default: false
    }
  },
  components: {
    STable
  },
  data () {
    return {
      dateFormat: 'YYYY/MM/DD',
      formItemLayout,
      loading: false,
      data: [],
      postParams: {
        hzfs: 'yx'
      },
      // 表头
      columns: [
        {
          title: '书目名称',
          dataIndex: 'smmc',
          scopedSlots: { customRender: 'smmc' }
        },
        {
          title: '书目作者',
          dataIndex: 'smzz',
          scopedSlots: { customRender: 'smzz' }
        },
        {
          title: '书目出版社',
          dataIndex: 'smcbs',
          scopedSlots: { customRender: 'smcbs' }
        },
        {
          title: '出版日期',
          dataIndex: 'cbrq',
          scopedSlots: { customRender: 'cbrq' }
        },
        {
          title: '操作',
          dataIndex: 'cz',
          fixed: 'right',
          width: 120,
          scopedSlots: { customRender: 'action' }
        }
      ],
      selectMenu: {
        yx: [] ,// 院系
        kmlx:[]
      }
    }
  },
  beforeCreate() {
  	this.form = this.$form.createForm(this)
  },
  computed: {
    ...mapState({
      systemInfo: state => state.app.systemInfo,
      TYPE0126: state => state.app['TYPE0126'] // 年级
    })
  },
  created () {
    this.initSelectMenu()
    selectArr.forEach(v => {
      this.$store.dispatch('app/setDictionary', v)
    })
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll()
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yx: res[0].data
        }
        this.$api.base.dictionaryData('TYPE0430').then(res => {
            this.selectMenu.kmlx = res.data
        })
      })
    },
    reset () {
      this.data = []
    },
    // 选择院系
    yxChange (code) {
      this.$set(this.queryParam, 'sszym', '')
      this.$api.base.majorAll({lsdwh: code}).then(res => {
        this.$set(this.selectMenu, 'zy', res.data)
      })
    },
    handleChange (value, key, column) {
      // console.log(value,key,column)
      const newData = [...this.data]
      const target = newData.filter(item => key === item.key)[0]
      if (target) {
        target[column] = value
        this.data = newData
      }
    },
    // 新增行
    handleAddLine () {
      this.data.push({
        smmc: '',
        smzz: '',
        smcbs: '',
        cbrq: '',
        key: Math.random().toString(26).substring(2)
      })
    },
    // 选择招生年度
    zsndChange () {

    },
    // 关闭
    onClose () {
      this.reset()
      this.form.resetFields()
      this.$emit('close')
    },
    // 设置时间
    handlePressChange (key, dataIndex, date, dateString) {
      const dataSource = [...this.data]
      const target = dataSource.find(item => item.key === key)
      if (target) {
        target[dataIndex] = dateString
        this.data = dataSource
      }
    },
    // 保存
    handleSubmit () {
      // console.log('表格数据', this.form.getFieldsValue())
       this.form.validateFields( (err,values) => {
        if(err){
          this.$message.error("请选择必填项")
        }else{
          var newObj = this.form.getFieldsValue()
          newObj.ssyxm = newObj.ssyxm.join(',')
          var params = Object.assign(newObj,{'tbZsSsckkmcks':JSON.stringify(this.data)})
          console.log(params)
          this.$api.recruitstat.entrySubjectManageAdd(params).then(res => {
            this.reset()
            this.form.resetFields()
            this.$emit('success')
          }).finally(() => {
            this.loadingBtn = false
          })
        }
      })
    },
    handleDelete (key) {
      const dataSource = [...this.data]
      this.data = dataSource.filter(item => item.key !== key)
    }
  }
}
</script>
